package com.loongtech.bi.action.biclient;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.client.EventAnalysisManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.*;
import com.loongtech.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 客户端分析 —事件分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/client/eventClient")
public class EventClientBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<EventClient> events;
	private ChartUIData eventsChart;
	private EventClient queryBean;  // 查询bean
	
	private Map<String, String> eventTypeItmes = GameParamConfigManager.getItems(CommonGameParam.EVENT_TYPE); // 事件类型
	private Map<String, String> eventTypeItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.EVENT_TYPE);
	
	private Map<String, String> eventNameItmes = GameParamConfigManager.getItems(CommonGameParam.EVENT_NAME); // 事件名称
	private Map<String, String> eventNameItmesRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.EVENT_NAME);
	//事件ID与事件类型ID
	private Map<String, String> eventIdEventTypeIdRevise = GameParamConfigManager.getItemsRevise(CommonGameParam.EVENTID_EVENTTYPEID);
	
	//事件类型id与事件id
	private Map<String,Set<String>> eventTypeIdEventIdMap;
	
	private String selectItem = "20";  // 首付等级选中的等级数
	@SuppressWarnings("serial")
	private LinkedHashMap<String, String> allItmes = new LinkedHashMap<String, String>() {{put("20", "20"); put("10", "10"); put("5", "5");}};  // 首付等级可选的等级数
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	@Autowired
	private EventAnalysisManager eventAnalysisManager;
	
	@PostConstruct
	public void init() {
		eventTypeIdEventIdMap = new HashMap<String, Set<String>>();
		queryBean = new EventClient();
		events = new ArrayList<EventClient>();
		
		//迭代
		for(Map.Entry<String,String> entry : eventIdEventTypeIdRevise.entrySet()) {
			String eventId = entry.getKey();
			String eventTypeId = entry.getValue();
			
			Set<String> set = eventTypeIdEventIdMap.get(eventTypeId);
			
			if(set == null) {
				set = new HashSet<String>();
			}
			
			set.add(eventId);
			//存入
			eventTypeIdEventIdMap.put(eventTypeId, set);
		}
	}
	
	//级联更新
	public void updateEventTypeName() {
		//如果不等于全部事件
		if(!queryBean.getEventId().equals(GameParamConfigManager.ALL_FLAT)) {
			//事件类型id
			String eventTypeId = eventIdEventTypeIdRevise.get(queryBean.getEventId());
			queryBean.setEventTypeId(eventTypeId);
		}
	}
	
	
	//级联更新
	public void updateEventName() {
		//如果不等于全部事件类型
		if(!queryBean.getEventTypeId().equals(GameParamConfigManager.ALL_FLAT)) {
			eventNameItmes = new LinkedHashMap<>();
			Set<String> set = eventTypeIdEventIdMap.get(queryBean.getEventTypeId());
			
			if(set != null) {
				//存入全部
				eventNameItmes.put("全部", GameParamConfigManager.ALL_FLAT);
				
				for(String eventId : set) {
					String evenetName = eventNameItmesRevise.get(eventId);
					eventNameItmes.put(evenetName, eventId);
				}
			}
		}else{
			eventNameItmes = GameParamConfigManager.getItems(CommonGameParam.EVENT_NAME);
		}
	}
	
	public void search() {
		sysLogManager.addLog(session.getUserName(), "客户端分析 —事件分析");
		events = new ArrayList<>();
		
		List<Object[]> entityEventAnalysisList = new ArrayList<Object[]>();
		
		//如果事件类型是全部 
		if(queryBean.getEventId().equals(GameParamConfigManager.ALL_FLAT) && 
		   queryBean.getEventTypeId().equals(GameParamConfigManager.ALL_FLAT)) {
			entityEventAnalysisList = eventAnalysisManager.getDetailEventAnalysis(dateStart, dateEnd, queryPlatform, queryChannel);
		}//如果事件类型不等于全部
		else if(!queryBean.getEventTypeId().equals(GameParamConfigManager.ALL_FLAT) &&
				queryBean.getEventId().equals(GameParamConfigManager.ALL_FLAT)) {
			//获得获得所有的事件id
			Set<String> set = eventTypeIdEventIdMap.get(queryBean.getEventTypeId());
			
			if(set != null) {
				StringBuffer sb = new StringBuffer();
				sb.append("(");
				
				int size = set.size();
				int index = 0;
				
				for(String stepcode : set) {
					index++;
					
					if(size == index) {
						sb.append(stepcode).append(")");
					}else {
						sb.append(stepcode).append(",");
					}
				}
				
				entityEventAnalysisList = eventAnalysisManager.getDetailEventAnalysisInStepcode(dateStart, dateEnd, queryPlatform,queryChannel,sb.toString());
			}
		}else {
			entityEventAnalysisList = eventAnalysisManager.getDetailEventAnalysisByStepcode(dateStart, dateEnd, queryPlatform,queryChannel,queryBean.getEventId());
		}
		
		for(Object[] o : entityEventAnalysisList) {
			EventClient e = new EventClient();
			e.setDateStr(o[0].toString());
			String eventTypeId = eventIdEventTypeIdRevise.get(o[1].toString());
			e.setEventTypeName(eventTypeItmesRevise.get(eventTypeId));
			e.setEventId(o[1].toString());
			e.setEventName(eventNameItmesRevise.get(o[1].toString()));
			e.setFinishnum(NumberUtil.parseInt(o[2].toString()));
			e.setUuidnum(NumberUtil.parseInt(o[3].toString()));
			e.setUseridnum(NumberUtil.parseInt(o[4].toString()));
			e.setRoleidnum(NumberUtil.parseInt(o[5].toString()));
			//加入
			events.add(e);
		}
		
		eventsChart = new ChartUIData();
		eventsChart.setJsonType(ChartType.COLUMN.type);
		//eventsChart.setJsonStr(new ChartTool(events, EventClient.class, "").loadData(eventsChart.getJsonType()));
	}
	
	public void handleEventsChange() {
		eventsChart = null;
		
		if (events != null && events.size() > 0) {
			eventsChart = new ChartUIData();
			eventsChart.setJsonType(ChartType.COLUMN.type);
			eventsChart.setJsonStr(new ChartTool(events, EventClient.class, "").loadData(eventsChart.getJsonType()));
		}
	}
	
	public String getEventType(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (eventTypeItmesRevise.get(key) == null) ? key : eventTypeItmesRevise.get(key); 
	}
	
	public String getEventName(Object itemType) {
		String key = itemType == null ? "" : Math.abs(Integer.parseInt(itemType.toString())) + "";
		return (eventNameItmesRevise.get(key) == null) ? key : eventNameItmesRevise.get(key); 
	}
	
	public class EventClient {
		private String dateStr; // 日期
		private String eventTypeId;
		private String eventId;
		private String eventTypeName; // 事件类型
		@ChartFlow(name = "事件名称")
		private String eventName; // 事件名称
		private int finishnum; //完成数量
		private int uuidnum; // 完成事件设备数
		private int useridnum; // 完成事件帐号数
		@ChartSelect(name = "完成事件角色数", key = 1)
		@ChartData1(name = "完成事件角色数", key = 1)
		private int roleidnum; // 完成事件角色数
		public String getDateStr() {
			return dateStr;
		}
		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}
		public String getEventTypeId() {
			return eventTypeId;
		}
		public void setEventTypeId(String eventTypeId) {
			this.eventTypeId = eventTypeId;
		}
		public String getEventTypeName() {
			return eventTypeName;
		}
		public void setEventTypeName(String eventTypeName) {
			this.eventTypeName = eventTypeName;
		}
		public String getEventName() {
			return eventName;
		}
		public void setEventName(String eventName) {
			this.eventName = eventName;
		}
		public int getFinishnum() {
			return finishnum;
		}
		public void setFinishnum(int finishnum) {
			this.finishnum = finishnum;
		}
		public int getUuidnum() {
			return uuidnum;
		}
		public void setUuidnum(int uuidnum) {
			this.uuidnum = uuidnum;
		}
		public int getUseridnum() {
			return useridnum;
		}
		public void setUseridnum(int useridnum) {
			this.useridnum = useridnum;
		}
		public int getRoleidnum() {
			return roleidnum;
		}
		public void setRoleidnum(int roleidnum) {
			this.roleidnum = roleidnum;
		}
		public String getEventId() {
			return eventId;
		}
		public void setEventId(String eventId) {
			this.eventId = eventId;
		}
	}
	
	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<EventClient> getEvents() {
		return events;
	}

	public void setEvents(List<EventClient> events) {
		this.events = events;
	}

	public LinkedHashMap<String, String> getAllItmes() {
		return allItmes;
	}

	public void setAllItmes(LinkedHashMap<String, String> allItmes) {
		this.allItmes = allItmes;
	}

	public ChartUIData getEventsChart() {
		return eventsChart;
	}

	public void setEventsChart(ChartUIData eventsChart) {
		this.eventsChart = eventsChart;
	}

	public String getSelectItem() {
		return selectItem;
	}

	public void setSelectItem(String selectItem) {
		this.selectItem = selectItem;
	}

	public Map<String, String> getEventTypeItmes() {
		return eventTypeItmes;
	}

	public void setEventTypeItmes(Map<String, String> eventTypeItmes) {
		this.eventTypeItmes = eventTypeItmes;
	}

	public Map<String, String> getEventTypeItmesRevise() {
		return eventTypeItmesRevise;
	}

	public void setEventTypeItmesRevise(Map<String, String> eventTypeItmesRevise) {
		this.eventTypeItmesRevise = eventTypeItmesRevise;
	}

	public Map<String, String> getEventNameItmes() {
		return eventNameItmes;
	}

	public void setEventNameItmes(Map<String, String> eventNameItmes) {
		this.eventNameItmes = eventNameItmes;
	}

	public Map<String, String> getEventNameItmesRevise() {
		return eventNameItmesRevise;
	}

	public void setEventNameItmesRevise(Map<String, String> eventNameItmesRevise) {
		this.eventNameItmesRevise = eventNameItmesRevise;
	}

	public EventClient getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(EventClient queryBean) {
		this.queryBean = queryBean;
	}
}
